/**
 * 读取本地存储
 * @param {String} key
 * @returns {any} value
 */
export const getStorage = (key) => {
  if (!key) return;
  const value = localStorage.getItem(key);
  if (!value) return null;
  return value.indexOf("{") === 0 || value.indexOf("[") === 0
    ? JSON.parse(value)
    : value;
};

/**
 * 写入本地存储
 * @param {String} key
 * @param {any} value
 */
export const setStorage = (key, value) => {
  if (!key) return;
  const data = typeof value === "object" ? JSON.stringify(value) : value;
  localStorage.setItem(key, data);
};

/**
 * 删除本地存储（单个）
 * @param {String} key
 */
export const removeStorage = (key) => {
  if (!key) return;
  localStorage.removeItem(key);
};

// 删除本地存储（全部）
export const removeAllStorage = () => {
  localStorage.clear();
};
